package com.test.fundible.fundible_05_process;

import static com.test.fundible.fundible_00_utils.FundibleUtil.startLoading;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.test.fundible.R;
import com.test.fundible.fundible_01_launcher.App_fundibleShow_fundibleCustomize;
import com.test.fundible.fundible_00_http.OkHttpUtil;
import com.test.fundible.fundible_00_utils.FundibleUtil;

public class Bin_fundibledAccount_fundibleDialog_Fr extends Dialog {

    private final String page_fundible;

    private final BindAcco_fundibleuntLis_fundibletener_Fr bindAccountListener_fundible;

    private final Acco_fundibleuntCha_fundiblennelViewAdapter accountChannelViewAdapter_fundible;


    public Bin_fundibledAccount_fundibleDialog_Fr(
            @NonNull FragmentActivity activity,
            String page,
            BindAcco_fundibleuntLis_fundibletener_Fr bindAccountListener) {
        super(activity);
        this.page_fundible = page;
        this.bindAccountListener_fundible = bindAccountListener;

        accountChannelViewAdapter_fundible = new Acco_fundibleuntCha_fundiblennelViewAdapter(App_fundibleShow_fundibleCustomize.getInstance_fundible().getAccountChannels_fundible(),page);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fundiblebindaccount_fr);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        findViewById(R.id.dbfa_view_out).setOnClickListener(v -> dismiss());
        findViewById(R.id.dbfa_view_in).setOnTouchListener((v, event) -> true);

        initView_fundible();
    }

    @SuppressLint("SetTextI18n")
    private void initView_fundible(){

        //初始化渠道选择器
        RecyclerView channelRv = findViewById(R.id.dbfa_rv_channel);
        channelRv.setAdapter(accountChannelViewAdapter_fundible);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);//网格布局管理器
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        channelRv.setLayoutManager(gridLayoutManager);

        //绑卡输入
        TextView areacode1Tv = findViewById(R.id.dbfa_tv_mobile1_areacode);
        TextView areacode2Tv = findViewById(R.id.dbfa_tv_mobile2_areacode);
        EditText mobile1Et = findViewById(R.id.dbfa_et_mobile1);
        EditText mobile2Et = findViewById(R.id.dbfa_et_mobile2);

        mobile1Et.setOnFocusChangeListener((v, hasFocus) ->
                OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"DIALOG_PHONE_INPUT",hasFocus ? "":mobile1Et.getText().toString().trim())
        );

        mobile2Et.setOnFocusChangeListener((v, hasFocus) ->
                OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"DIALOG_PHONE_CONFIRM_INPUT",hasFocus ? "":mobile2Et.getText().toString().trim())
        );

        areacode1Tv.setText(FundibleUtil.getAreaCode_fundible());
        areacode2Tv.setText(FundibleUtil.getAreaCode_fundible());

        //绑卡提交
        findViewById(R.id.dbfa_btn_add).setOnClickListener(v -> {

            OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"DIALOG_SAVE_CLICK","");

            String mobile1 = mobile1Et.getText().toString().trim();
            String mobile2 = mobile2Et.getText().toString().trim();

            if (TextUtils.isEmpty(mobile1)) mobile1 = "";
            if (TextUtils.isEmpty(mobile2)) mobile2 = "";

            AccountCha_fundiblenn_fundibleel_Fr selectChannel = accountChannelViewAdapter_fundible.getSelectChannel_fundible();
            String channel = selectChannel==null ? "":selectChannel.getValue_fundible();

            String finalMobile = mobile1;

            startLoading(getContext(),"");
            OkHttpUtil.getInstance_fundible().addWalletAccount_Fr(
                    mobile1,
                    mobile2,
                    channel,
                    () -> {if (bindAccountListener_fundible !=null) bindAccountListener_fundible.onBindAccount_fundible(finalMobile);},
                    false
            );

            dismiss();

        });

    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"DIALOG_CLOSE_CLICK","");
    }

}
